﻿using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
 
using System.Collections;


public partial class Manageweb_Member_MemExchangeCurrency : System.Web.UI.Page
{
    public string StrKey;

    protected override void Render(HtmlTextWriter writer)
    {
        System.IO.StringWriter sw = new System.IO.StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        base.Render(htw);

        if (!hidMiYao.Value.Equals("1"))
        {
            writer.Write(sw.ToString().Replace("<body", "<body onload=javascript:openLayer('test','test_con');"));
        }
        else
        {
            writer.Write(sw);
        }
    }


    //获取密钥Cdefine5
    public string CheckData()
    {
        SqlParameter[] paras ={ 
                new SqlParameter("@userid",SqlDbType.VarChar,30)
            };
        paras[0].Value = Session["userid"];

        StrKey = SQLExpressDAL.SqlHelper.FillDataSet(CommandType.StoredProcedure, "UP_GetUserKey", paras).Tables[0].Rows[0][1].ToString();
        return StrKey;
    }

    protected void Page_Load(object sender, EventArgs e)
    {

        StrKey = CheckData();
        if (hidMiYao.Value.Equals("1"))
        {
            this.transferMoney.Visible = true;
            InitAccountInfo();
        }
        
    }

    protected void InitAccountInfo()
    {
        this.txtUserAccount.Text = Session["UserID"].ToString();
    }


    protected void btnUpdate_Command(object sender, CommandEventArgs e)
    {
        String[] userInfos = e.CommandArgument.ToString().Split('|');

        this.txtUserAccount.Text = userInfos[0];
        this.txtReceiveAccount.Text = userInfos[1];


        this.TABLE_Add_Mod.Visible = true;
    }

    //protected void BindUsers()
    //{
    //    SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
    //    this.GV_UserList.DataSource = adp.GetOtherUserList(Session["UserID"].ToString());
    //    this.GV_UserList.DataBind();
    //}

    protected void Btn_Submit_Click(object sender, EventArgs e)
    {
        //检查账号是否存在
        if (!CheckReceiveIsExist())
        {
            return;
        }
        //查看是否转账给自己
        if (!CheckReciverIsSelf())
        {
            return;
        }
        //检查账户金额
        if (CheckAccountMoney())
        {

            BeginExchange();
        }


        txtClear();
        //GetAccountInfo();
        GetUserAccountInfo();

    }
    //检查转入账号是否存在
    protected bool CheckReceiveIsExist()
    {
        bool isExists = false;

        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter adp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        int flag = (int)adp.CheckUserIsExist(this.txtReceiveAccount.Text.Trim());

        if (flag == 1)
        {
            isExists = true;
        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('账号：" + this.txtReceiveAccount.Text + "不存在请确保输入正确!');", true);
        }

        return isExists;
    }

    //禁止转入用户是自己
    protected bool CheckReciverIsSelf()
    {
        bool flag = true;

        if (this.txtReceiveAccount.Text.Trim().Equals(this.txtUserAccount.Text.Trim()))
        {
            flag = false;
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('不能把钱转给自己');", true);
        }
        return flag;
    }

    //检查交易金额是否过大
    protected bool CheckAccountMoney()
    {
        bool flag = true;

        //  Label lblMoney = Master.FindControl("lblAccountMoney") as Label;
        Label lblMoney = Master.FindControl("lblAB1") as Label;
        Decimal accountMoney = Convert.ToDecimal(lblMoney.Text);
        Decimal exchangeMoney = Convert.ToDecimal(this.txtVCurrency.Text.Trim());

        if (exchangeMoney > accountMoney)
        {

            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('转出金额超过账户金额');", true);
            flag = false;
        }

        return flag;
    }

    protected void BeginExchange()
    {
        string fromUserAccount = Session["UserID"].ToString();
        string toUserAccount = this.txtReceiveAccount.Text.Trim();
        string exchangeMoney = this.txtVCurrency.Text.Trim();
        string msg = "";

        try
        {
            SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
            int flag = obj.ExChangeMoney(fromUserAccount, toUserAccount, exchangeMoney);

            msg = flag > 0 ? "转账成功" : "转账失败";

        }
        catch (Exception ex)
        {
            msg = "转账失败";
        }
        finally
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('" + msg + "');", true);
        }
    }

    protected void txtClear()
    {
        this.txtUserAccount.Text = "";
        this.txtReceiveAccount.Text = "";
        this.txtVCurrency.Text = "";
    }

    //计算重新显示账户金额 
    public void GetAccountInfo()
    {
        SQLExpressDAL.UsersTableAdapters.UsersTableAdapter userAdp = new SQLExpressDAL.UsersTableAdapters.UsersTableAdapter();
        SQLExpressDAL.Users.UsersDataTable tab = userAdp.GetSingleUserInfo(Session["UserID"].ToString());

        DataRow dr = tab.Rows[0];


        Label lblUserName = Master.FindControl("lblUserName") as Label;

        Label lblAccount = Master.FindControl("lblAccount") as Label;


        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        Hashtable htab = obj.GetMemAccountMoney(Session["UserID"].ToString());

        if (htab["IsSuccess"].ToString().Equals("1"))
        {
            Label lblAccountMoney = Master.FindControl("lblAccountMoney") as Label;

            //lblAccountMoney.Text = htab["Count"].ToString();

        }
        else
        {
            Page.ClientScript.RegisterStartupScript(this.GetType(), "msg", "alert('金额统计错误');", true);
            return;
        }


        Label LjScore = Master.FindControl("lblLjScore") as Label;
        LjScore.Text = dr["ljscore"].ToString();
        Label SpecialScore = Master.FindControl("lblSpecialScore") as Label;
        SpecialScore.Text = dr["specialscore"].ToString();

    }

    /// <summary>
    /// 初始化账户的金额信息(左侧列表)
    /// </summary>
    protected void GetUserAccountInfo()
    {
        SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter adp = new SQLExpressDAL.TransferRecordsTableAdapters.UsersAccountTableAdapter();
        SQLExpressDAL.TransferRecords.UsersAccountDataTable tab = adp.GetSingleAccount(Session["UserID"].ToString());

        SQLExpressDAL.TransferRecords.UsersAccountRow dr = (SQLExpressDAL.TransferRecords.UsersAccountRow)tab.Rows[0];

        Label lblAB1 = Master.FindControl("lblAB1") as Label;

        Label lblAB2 = Master.FindControl("lblAB2") as Label;

        Label lblAB3 = Master.FindControl("lblAB3") as Label;

        Label lblAB4 = Master.FindControl("lblAB4") as Label;

        lblAB1.Text = dr.AB1.ToString("f2");
        lblAB2.Text = dr.AB2.ToString("f2");
        lblAB3.Text = dr.AB3.ToString("f2");

        lblAB4.Text = dr.AB4.ToString("f2");
    }

    protected void BindTransferRecord()
    {

        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        this.GV_TransferRecord.DataSource = obj.GetMemTransferRecord(Session["UserID"].ToString(), this.txtBeginDate.Text, this.txtEndDate.Text, "zhuanzhang", this.ddlTransferType.SelectedValue);
        this.GV_TransferRecord.DataBind();
    }

    protected void GV_TransferRecord_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        this.GV_TransferRecord.PageIndex = e.NewPageIndex;
        BindTransferRecord();
    }
    protected void btnQuery_Click(object sender, EventArgs e)
    {
        SQLExpressDAL.DealRecord obj = new SQLExpressDAL.DealRecord();
        this.GV_TransferRecord.DataSource = obj.GetMemTransferRecord(Session["UserID"].ToString(), this.txtBeginDate.Text, this.txtEndDate.Text, "zhuanzhang", this.ddlTransferType.SelectedValue);
        this.GV_TransferRecord.DataBind();
    }
}
